import type { App } from "vue";
// 引入 test.ts
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
// 引入 nprogress 相关方法
import { close, start } from "@/utils/nprogress";
import routerList from "@/router/routerList";
export const publicRoutes: Array<typeof RouteRecordRaw> = [...routerList];

// 定义router
const router = createRouter({
  history: createWebHashHistory(),
  routes: publicRoutes,
});

// 路由前置后卫
router.beforeEach(() => {
  // 开启进度条
  start();
});
// 路由后置后卫
router.afterEach(() => {
  // 关闭进度条
  close();
});

/* 初始化路由表 */
export function resetRouter() {
  // @ts-ignore
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export default router;
export const setupRouter = (app: App<Element>) => {
  app.use(router);
};
